local null_ls = require "null-ls"
local helpers = require "null-ls.helpers"

return {
  method = null_ls.methods.DIAGNOSTICS,
  filetypes = { "xidoc" },
  generator = helpers.generator_factory {
    command = "xidoc",
    args = { "--dry-run", "--force-stdin", "--no-color", "$FILENAME" },
    to_stdin = true,
    from_stderr = true,
    format = "raw",
    on_output = function(params, done)
      if not params.output then
        done {}
      end
      local row, col, end_row, end_col, message = params.output:match "\nat (%d+):(%d+)-(%d+):(%d+)[^\n]*\n([A-Z].*)"
      if message then
        local diagnostic = {
          row = tonumber(row),
          col = tonumber(col),
          end_row = tonumber(end_row),
          end_col = tonumber(end_col) + 1,
          message = message,
        }
        done { diagnostic }
      else
        done {}
      end
    end,
  },
}
